package com.tools.web.email;

import com.tools.common.object.Note;
import com.tools.common.object.string.VerifyKit;

import java.util.Objects;

/**
 * 邮件接收方的用户实例
 * */
@Note("邮件接收方的用户实例")
public final class MailUser {

    @Note("接收方的邮箱账号")
    private String mail;

    @Note("发送邮箱的方式，CC-抄送、BCC-密送、TO-正常发送")
    private MailSendEnum method = MailSendEnum.TO;


    public MailUser(String mail) {
        this.setMail(mail);
    }

    public MailUser(String mail, MailSendEnum method) {
        this.setMail(mail);
        this.setMethod(method);
    }

    public static MailUser of(String mail) {
        return new MailUser(mail);
    }

    public static MailUser of(String mail, MailSendEnum method) {
        return new MailUser(mail, method);
    }

    /* **********************************************************************************
     *
     *          Getter / Setter
     *
     * *********************************************************************************
     * */

    public String getMail() {
        return mail;
    }

    public void setMail(String mail) {
        if(VerifyKit.isEmail(mail)) {
            this.mail = mail;
            return;
        }
        throw new IllegalArgumentException("【" + mail + "】 不是标准的邮箱地址");
    }

    public MailSendEnum getMethod() {
        return method;
    }

    public void setMethod(MailSendEnum method) {
        if(method == null) throw new NullPointerException("不知道 null 是什么发送方式");
        this.method = method;
    }

    @Override
    public String toString() {
        return "MailUser{" +
                "mail='" + mail + '\'' +
                ", method=" + method +
                '}';
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        MailUser mailUser = (MailUser) o;
        return Objects.equals(mail, mailUser.mail) && method == mailUser.method;
    }

    @Override
    public int hashCode() {
        return Objects.hash(mail, method);
    }

    public MailUser mail(String mail) {
        this.setMail(mail);
        return this;
    }

    public MailUser method(MailSendEnum method) {
        this.setMethod(method);
        return this;
    }
}
